package com.bysc.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bysc.userservice.entity.UserCollege;
import com.bysc.userservice.entity.UserMajor;
import com.bysc.userservice.entity.vo.CollegeAndMajorVo;
import com.bysc.userservice.mapper.UserCollegeMapper;
import com.bysc.userservice.service.UserCollegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bysc.userservice.service.UserMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kkk
 * @since 2024-05-20
 */
@Service
public class UserCollegeServiceImpl extends ServiceImpl<UserCollegeMapper, UserCollege> implements UserCollegeService {
    @Autowired
    private UserMajorService userMajorService;
    @Override
    public List getAllCollegeAndMajor() {
        List<UserCollege> collegeList = this.list(new QueryWrapper<UserCollege>().orderByDesc("sort"));
        List<UserMajor> majorList = userMajorService.list(new QueryWrapper<UserMajor>().orderByDesc("sort"));
        ArrayList<CollegeAndMajorVo> list = new ArrayList<>();
        CollegeAndMajorVo collegeAndMajorVo = null;
        for (UserCollege college : collegeList) {
            collegeAndMajorVo = new CollegeAndMajorVo();
            collegeAndMajorVo.setCollegeId(college.getId());
            collegeAndMajorVo.setCollegeName( college.getName());
            ArrayList<UserMajor> majors = new ArrayList<>();
            for (UserMajor major : majorList) {
                if (major.getCollegeId().equals(college.getId())) {
                    majors.add(major);
                }
            }
            collegeAndMajorVo.setMajorList(majors);
            list.add(collegeAndMajorVo);
        }
        return list;
    }
}
